package com.kantboot.security.web.controller;

import com.kantboot.security.service.ISecurityRequestService;
import com.kantboot.util.common.result.RestResult;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/security/request")
public class SecurityRequestController {

    @Resource
    private ISecurityRequestService service;

    /**
     * 获取新的公钥
     * @return 公钥
     */
    @RequestMapping("/getNewPublicKey")
    public RestResult<String> getNewPublicKey() {
        return RestResult.success(service.getNewPublicKey(), "getSuccess", "获取成功");
    }

    /**
     * 转发请求
     */
    @RequestMapping("/simpleForward/**")
    public RestResult<String> simpleForward(@RequestParam("content") String content, HttpServletRequest request) {
        // 获取全部uri
        String requestURI = request.getRequestURI();
        // 获取simpleForward后的uri
        String uri = requestURI.substring(requestURI.indexOf("/simpleForward/")+"/simpleForward/".length()-1);
        // 默认所有请求都是post请求
        String method = "POST";
        // 分为三个.分割，第一个是后端生成的公钥，第二个前端加密后的请求体，第三个是客户端生成的公钥
        String[] split = content.split("\\.");

        return RestResult.success(service.simpleForward(content), "forwardSuccess", "转发成功");
    }
}
